﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;

namespace AcessaDadosBD.Repositorios
{
    public class IntervaloLoteRepository : IRepository<IntervaloLote>
    {
        private IDbCommand Command { get; set; }

        public IntervaloLoteRepository(IDbCommand cmd)
        {
            this.Command = cmd;
        }

        public IntervaloLote GetById(int id)
        {
            try
            {
                Command.Connection.Open();

                Command.CommandText = "SELECT idIntervaloLote, idOperadoraPrestador, inicio, fim, atual FROM tbIntervaloLote WHERE idIntervaloLote=" + id;
                Command.CommandType = CommandType.Text;

                var reader = Command.ExecuteReader();

                if (reader.Read())
                {
                    IntervaloLote intervaloLote = new IntervaloLote()
                    {
                        ID = int.Parse(reader["idIntervaloLote"].ToString()),
                        IDOperadoraPrestador = int.Parse(reader["idOperadoraPrestador"].ToString()),
                        Inicio = int.Parse(reader["inicio"].ToString()),
                        Fim = int.Parse(reader["fim"].ToString()),
                        Atual = int.Parse(reader["atual"].ToString())
                    };
                    return intervaloLote;
                }
                else
                {
                    throw new Exception("IntervaloLote não encontrado");
                }
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao executar operação: " + e.Message);
            }
            finally
            {
                Command.Connection.Close();
            }
        }

        public IQueryable<IntervaloLote> GetAll()
        {
            try
            {
                Command.Connection.Open();

                Command.CommandText = "SELECT idIntervaloLote, idOperadoraPrestador, inicio, fim, atual FROM tbIntervaloLote";
                Command.CommandType = CommandType.Text;

                var reader = Command.ExecuteReader();

                List<IntervaloLote> list = new List<IntervaloLote>();
                while (reader.Read())
                {
                    IntervaloLote intervaloLote = new IntervaloLote()
                    {
                        ID = int.Parse(reader["idIntervaloLote"].ToString()),
                        IDOperadoraPrestador = int.Parse(reader["idOperadoraPrestador"].ToString()),
                        Inicio = int.Parse(reader["inicio"].ToString()),
                        Fim = int.Parse(reader["fim"].ToString()),
                        Atual = int.Parse(reader["atual"].ToString())
                    };
                    list.Add(intervaloLote);
                }
                return list.AsQueryable<IntervaloLote>();
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao executar operação: " + e.Message);
            }
            finally
            {
                Command.Connection.Close();
            }
        }

        public int Insert(IntervaloLote entity)
        {
            try
            {
                Command.Connection.Open();

                Command.CommandText = "INSERT INTO tbIntervaloLote (idOperadoraPrestador, inicio, fim, atual) VALUES (" +
                    entity.IDOperadoraPrestador + "," + entity.Inicio + "," + entity.Fim + ", " + entity.Atual + ")";
                Command.CommandType = CommandType.Text;

                Command.ExecuteNonQuery();

                Command.CommandText = "SELECT @@IDENTITY";

                return int.Parse(Command.ExecuteScalar().ToString());
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao executar operação: " + e.Message);
            }
            finally
            {
                Command.Connection.Close();
            }
        }

        public void Delete(IntervaloLote entity)
        {
            try
            {
                Command.Connection.Open();

                Command.CommandText = "DELETE FROM tbIntervaloLote WHERE idIntervaloLote=" + entity.ID;
                Command.CommandType = CommandType.Text;

                Command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao executar operação: " + e.Message);
            }
            finally
            {
                Command.Connection.Close();
            }
        }

        public void Update(IntervaloLote entity)
        {
            try
            {
                Command.Connection.Open();

                Command.CommandText = "UPDATE tbIntervaloLote SET idOperadoraPrestador=" + entity.IDOperadoraPrestador + ", inicio=" + entity.Inicio +
                    ", fim=" + entity.Fim + ", atual=" + entity.Atual + " WHERE idIntervaloLote=" + entity.ID;
                Command.CommandType = CommandType.Text;

                Command.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception("Erro ao executar operação: " + e.Message);
            }
            finally
            {
                Command.Connection.Close();
            }
        }
    }
}
